import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    BeforeInsert
} from 'typeorm';
import * as bcrypt from 'bcryptjs'
export enum Sex {
    '女' = 0,
    '男' = 1,
    '' = 2,
}
@Entity('im_user') // 指定表名称为 'im_user'
export class User {

    @PrimaryGeneratedColumn({ comment: '自增主键 主键id', })
    user_id: string;
    // 姓名
    @Column({ type: 'varchar', name: 'name', comment: '姓名', length: 50, })
    name: string;
    // 密码 @Column({ select: false})    // 表示查询过程中隐藏此列
    @Column({ type: 'varchar', name: 'password', comment: '密码', length: 200, select: false })
    password: string;
    // phone手机号
    @Column({ comment: '手机号', length: 20 })
    phone: string;
    // -----------------以下是新郑字段---------------  

    @Column({ comment: '性别', nullable: true, default: "1", length: 1 })
    sex: string;
    // avatar头像
    @Column({ comment: '头像', nullable: true })//非必填项
    avatar: string;
    @Column({ comment: '昵称', length: 100, nullable: true })
    nickname: string;  //昵称

    // 邮箱
    @Column({ comment: '邮箱', nullable: true, length: 100 })
    email: string;



    // 创建时间
    @Column({
        comment: '创建时间',
        name: 'create_time',
        type: 'timestamp',
        default: () => 'CURRENT_TIMESTAMP',
    })
    createTime: Date;

    //更新时间
    @Column({
        comment: '更新时间',
        name: 'update_time',
        type: 'timestamp',
        default: () => 'CURRENT_TIMESTAMP',
    })
    updateTime: Date;


    // 执行之前处理函数
    @BeforeInsert()
    async encryptPassword() {
        if (this.password) {
            this.password = await bcrypt.hashSync(this.password, 10);
        }
    }

}



